package com.wink_172.library.utils

import android.util.Log
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 * 时间获取 转换相关
 */
object DateUtil {

    const val FORMAT_YEAR = "yyyy"
    const val FORMAT_MONTH_DAY = "M月d日"
    const val FORMAT_DATE = "yyyy-M-d"
    const val FORMAT_TIME = "HH:mm"
    const val FORMAT_MONTH_DAY_TIME = "M 月 d 日  HH:mm" // 1636005969000  HH为大写14:06  hh为小写02:06
    const val FORMAT_DATE_TIME = "yyyy-M-d HH:mm:ss"
    const val FORMAT_DATE_YUENAN_TIME = "HH:mm:ss dd-MM-yyyy"
    const val FORMAT_DATE_TIME02 = "yyyyMd HH:mm"
    const val FORMAT_DATE_TIME03 = "yyyy 年 M 月 d 日 HH:mm"
    const val FORMAT_DATE_TIME04 = "M/d HH:mm"
    const val FORMAT_DATE1_TIME = "yyyy/M/d HH:mm"
    const val FORMAT_DATE_TIME_SECOND = "yyyy/M/d HH:mm:ss"
    const val FORMAT_DATE_TIME_SECOND02 = "yyyyMdHmmss"
    private val formatDate = SimpleDateFormat("yyyy-M-d", Locale.getDefault())
    private val formatDay = SimpleDateFormat("d", Locale.getDefault())
    private val formatMonthDay = SimpleDateFormat("M-d", Locale.getDefault())
    private val formatDateTime = SimpleDateFormat("yyyy-M-d HH:mm:ss", Locale.getDefault())
    private val sdf = SimpleDateFormat()
    private const val YEAR = 365 * 24 * 60 * 60 // 年
    private const val MONTH = 30 * 24 * 60 * 60 // 月
    private const val DAY = 24 * 60 * 60 // 天
    private const val HOUR = 60 * 60 // 小时
    private const val MINUTE = 60 // 分钟

    /**
     * currentTime 毫秒级别时间戳
     * formatType要转换的string类型的时间格式
     */
    @JvmStatic
    fun longToString(currentTime: Long, formatType: String?): String {
        val strTime: String
        // long类型转成Date类型
        val date = longToDate(currentTime, formatType)
        // date类型转成String
        strTime = dateToString(date, formatType)
        return strTime
    }

    @JvmStatic
    fun getTime02(date: Date?, fotmat: String?): String { //可根据需要自行截取数据显示
        val format = SimpleDateFormat(fotmat)
        return format.format(date)
    }

    @JvmStatic
    fun getTime(date: Date?): String { //可根据需要自行截取数据显示
        val format = SimpleDateFormat(FORMAT_DATE_TIME)

        //        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date)
    }

//    @JvmStatic
//    fun getTimeYear(date: Date?): String { //可根据需要自行截取数据显示
////        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        val format = SimpleDateFormat("yyyy")
//        return format.format(date)
//    }
//
//    @JvmStatic
//    fun getTimeMonth(date: Date?): String { //可根据需要自行截取数据显示
////        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        val format = SimpleDateFormat("MM")
//        return format.format(date)
//    }

    /**
     * 根据时间戳获取描述性时间，如3分钟前，1天前
     *
     * @param timestamp 时间戳 单位为毫秒
     * @return 距离秒字符串
     */
    @JvmStatic
    fun getDescriptionTimeFromTimestamp02(timestamp: Long): Long {
        val currentTime = System.currentTimeMillis()
        // 与现在时间相差总秒数
        val timeGap = (currentTime - timestamp) / 1000
        Log.e(
            TAG,
            "getDescriptionTimeFromTimestamp02:====>> timestamp:$timestamp     currentTime:$currentTime    timeGap：$timeGap"
        )
        return timeGap
    }

    @JvmStatic
    fun getTimeDay(date: Date?): String { //可根据需要自行截取数据显示
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        val format = SimpleDateFormat("dd")
        return format.format(date)
    }

    @JvmStatic
    fun getImageTime(time: Long): String {
        val calendar = Calendar.getInstance()
        calendar.time = Date()
        val imageTime = Calendar.getInstance()
        imageTime.timeInMillis = time
        return if (sameDay(calendar, imageTime)) {
            "今天"
        } else if (sameWeek(calendar, imageTime)) {
            "本周"
        } else if (sameMonth(calendar, imageTime)) {
            "本月"
        } else {
            val date = Date(time)
            val sdf = SimpleDateFormat("yyyy/MM")
            sdf.format(date)
        }
    }

    @JvmStatic
    fun sameDay(calendar1: Calendar, calendar2: Calendar): Boolean {
        return (calendar1[Calendar.YEAR] == calendar2[Calendar.YEAR]
                && calendar1[Calendar.DAY_OF_YEAR] == calendar2[Calendar.DAY_OF_YEAR])
    }

    @JvmStatic
    fun sameWeek(calendar1: Calendar, calendar2: Calendar): Boolean {
        return (calendar1[Calendar.YEAR] == calendar2[Calendar.YEAR]
                && calendar1[Calendar.WEEK_OF_YEAR] == calendar2[Calendar.WEEK_OF_YEAR])
    }

    @JvmStatic
    fun sameMonth(calendar1: Calendar, calendar2: Calendar): Boolean {
        return (calendar1[Calendar.YEAR] == calendar2[Calendar.YEAR]
                && calendar1[Calendar.MONTH] == calendar2[Calendar.MONTH])
    }

    /**
     * 根据时间戳获取描述性时间，如3分钟后，1天后
     *
     * @param timestamp 时间戳 单位为毫秒
     * @return 时间字符串
     */
    @JvmStatic
    fun getDescriptionTimeFromTimestamp3(timestamp: Long): String {
        val currentTime = System.currentTimeMillis()
        // 与现在时间相差秒数
        val timeGap = (timestamp - currentTime) / 1000
        println("timeGap: $timeGap")
        val timeStr: String
        timeStr = if (timeGap > YEAR) {
            (timeGap / YEAR).toString() + "年后"
        } else if (timeGap > MONTH) {
            (timeGap / MONTH).toString() + "个月后"
        } else if (timeGap > DAY) { // 1天以上
            (timeGap / DAY).toString() + "天后"
        } else if (timeGap > HOUR) { // 1小时-24小时
            (timeGap / HOUR).toString() + "小时后"
        } else if (timeGap > MINUTE) { // 1分钟-59分钟
            (timeGap / MINUTE).toString() + "分钟后"
        } else { // 1秒钟-59秒钟
            "刚刚"
        }
        return timeStr
    }

    /**
     * 根据时间戳获取描述性时间，如3分钟前，1天前
     *
     * @param timestamp 时间戳 单位为毫秒
     * @return 时间字符串
     */
    @JvmStatic
    fun getDescriptionTimeFromTimestamp(timestamp: Long): String {
        val currentTime = System.currentTimeMillis()
        // 与现在时间相差秒数
        val timeGap = (currentTime - timestamp) / 1000
        println("timeGap: $timeGap")
        val timeStr: String
        timeStr = if (timeGap > YEAR) {
            (timeGap / YEAR).toString() + "年前"
        } else if (timeGap > MONTH) {
            (timeGap / MONTH).toString() + "个月前"
        } else if (timeGap > DAY) { // 1天以上
            (timeGap / DAY).toString() + "天前"
        } else if (timeGap > HOUR) { // 1小时-24小时
            (timeGap / HOUR).toString() + "小时前"
        } else if (timeGap > MINUTE) { // 1分钟-59分钟
            (timeGap / MINUTE).toString() + "分钟前"
        } else { // 1秒钟-59秒钟
            "刚刚"
        }
        return timeStr
    }

    /**
     * 获取当前日期的指定格式的字符串
     *
     * @param format 指定的日期时间格式，若为null或""则使用指定的格式"yyyy-MM-dd HH:MM"
     */
    @JvmStatic
    fun getCurrentTime(format: String?): String {
        if (format == null || format.trim { it <= ' ' } == "") {
            sdf.applyPattern(FORMAT_DATE_TIME)
        } else {
            sdf.applyPattern(format)
        }
        return sdf.format(Date())
    }

    /**
     * date类型转换为String类型
     * formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
     * data Date类型的时间
     */
    @JvmStatic
    fun dateToString(data: Date?, formatType: String?): String {
        return SimpleDateFormat(formatType).format(data)
    }



    /**
     * string类型转换为date类型
     * strTime要转换的string类型的时间，formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
     * HH时mm分ss秒，
     * strTime的时间格式必须要与formatType的时间格式相同
     */
    @JvmStatic
    fun stringToDate(strTime: String?, formatType: String?): Date? {
        val formatter = SimpleDateFormat(formatType)
        var date: Date? = null
        try {
            date = formatter.parse(strTime)
        } catch (e: ParseException) {
            // TODO Auto-generated catch block
            e.printStackTrace()
        }
        return date
    }

    /**
     * long转换为Date类型
     * currentTime要转换的long类型的时间
     * formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
     */
    @JvmStatic
    fun longToDate(currentTime: Long, formatType: String?): Date? {
        // 根据long类型的毫秒数生命一个date类型的时间
        val dateOld = Date(currentTime)
        // 把date类型的时间转换为string
        val sDateTime = dateToString(dateOld, formatType)
        // 把String类型转换为Date类型
        return stringToDate(sDateTime, formatType)
    }

    /**
     * string类型转换为long类型
     * strTime要转换的String类型的时间
     * formatType时间格式
     * strTime的时间格式和formatType的时间格式必须相同
     * 输出毫秒级别时间戳
     */
    @JvmStatic
    fun stringToLong(strTime: String?, formatType: String?): Long {
        // String类型转成date类型
        val date = stringToDate(strTime, formatType)
        return if (date == null) {
            0
        } else {
            // date类型转成long类型
            dateToLong(date)
        }
    }

    /**
     * date类型转换为long类型
     * date要转换的date类型的时间
     */
    @JvmStatic
    fun dateToLong(date: Date): Long {
        return date.time
    }

    private const val TAG = "TAG"



    /**
     * 时间戳转换成Calendar
     * timeStamp为秒级别
     * 最需需要调用 inputTime.get(Calendar.DAY_OF_MONTH)方法获取真实值
    calendar[java.util.Calendar.YEAR],
    calendar[java.util.Calendar.MONTH] + 1,
    calendar[java.util.Calendar.DAY_OF_MONTH]


    calendar.add(java.util.Calendar.MONTH, +2)//加两月
     * */
    @JvmStatic
    fun longToCalendar(timeStamp: Long): Calendar { //根据时间戳获取时间   success
        val inputTime = Calendar.getInstance()
        inputTime.timeInMillis = timeStamp * 1000
        //        Log.e(TAG, "parseSetAlarmResult: ====>>" + inputTime.get(Calendar.YEAR) + " " + inputTime.get(Calendar.MONTH)
//                + " " + inputTime.get(Calendar.DAY_OF_MONTH) + " " +
//                inputTime.get(Calendar.HOUR_OF_DAY) + " " + inputTime.get(Calendar.MINUTE));
        return inputTime
    }


    @JvmStatic
    fun getCalendar(
        year: Int,
        month: Int,
        day: Int,
        hour: Int,
        minute: Int,
        second: Int
    ): Calendar {
        val calendar = Calendar.getInstance()
        calendar[Calendar.YEAR] = year
        calendar[Calendar.MONTH] = month //month-1
        calendar[Calendar.DAY_OF_MONTH] = day
        calendar[Calendar.HOUR_OF_DAY] = hour
        calendar[Calendar.MINUTE] = minute
        calendar[Calendar.SECOND] = second
        return calendar
    }

    //获取当天的时间
    @JvmStatic
    fun getTimeStrInDay(timeStamp: Long): Array<String?> { //7:30 AM
        val inputTime = Calendar.getInstance()
        inputTime.timeInMillis = timeStamp * 1000
        val strings = arrayOfNulls<String>(2)
        val date = inputTime.time
        var hour = date.hours
        val mintues = date.minutes
        val m = "" + if (mintues < 10) "0$mintues" else mintues
        if (hour >= 12) {
            hour = hour - 12
            val h = "" + if (hour < 10) "0$hour" else hour
            strings[0] = "$h:$m"
            strings[1] = "PM"
        } else {
            val h = "" + if (hour < 10) "0$hour" else hour
            strings[0] = "$h:$m"
            strings[1] = "AM"
        }
        return strings
    }

    /**
     * 时间转化为聊天界面显示字符串
     *
     * @param timeStamp 单位为秒
     */
    @JvmStatic
    fun getChatTimeStr(timeStamp: Long): String {
        if (timeStamp == 0L) return ""
        val inputTime = Calendar.getInstance()
        inputTime.timeInMillis = timeStamp * 1000
        val currenTimeZone = inputTime.time
        val calendar = Calendar.getInstance()
        if (!calendar.after(inputTime)) {
            //当前时间在输入时间之前   请注意可能是用户手动设置了自定义时间-没对上北京时间
            val sdf = SimpleDateFormat("yyyy" + "-" + "MM" + "-" + "dd" )
            return sdf.format(currenTimeZone)
        }
        calendar[Calendar.HOUR_OF_DAY] = 0
        calendar[Calendar.MINUTE] = 0
        calendar[Calendar.SECOND] = 0
        calendar[Calendar.MILLISECOND] = 0
        if (calendar.before(inputTime)) {
            val sdf = SimpleDateFormat("HH:mm")
            return sdf.format(currenTimeZone)
        }
        calendar.add(Calendar.DAY_OF_MONTH, -1)
        return if (calendar.before(inputTime)) {
//            val sdf = SimpleDateFormat("HH:mm")
//            "昨天" + " " + sdf.format(currenTimeZone)
            val sdf = SimpleDateFormat("MM" + "-" + "dd" + " " + "HH:mm")
           sdf.format(currenTimeZone)

        } else {
            calendar[Calendar.DAY_OF_MONTH] = 1
            calendar[Calendar.MONTH] = Calendar.JANUARY
            if (calendar.before(inputTime)) {
                val sdf = SimpleDateFormat("MM" + "-" + "dd" + " " + "HH:mm")
                sdf.format(currenTimeZone)
            } else {
                val sdf = SimpleDateFormat("yyyy" + "-" + "MM" + "-" + "dd" + " " + "HH:mm")
                sdf.format(currenTimeZone)
            }
        }
    }


    /**
     * 时间戳转化为聊天界面显示字符串
     * 少于 60 秒：刚刚
    少于 60 分钟：xx 分钟前
    今天并大于 60 分钟：hh:mm
    昨天：昨天 hh:mm
    今年并大于昨天：MM-DD hh:mm
    大于今年：YYYY-MM-DD hh:mm
     *
     * @param timeStamp 单位为秒
     */
    @JvmStatic
    fun getChatTimeStr2(timeStamp: Long): String {
        val currentTime = System.currentTimeMillis()
        // 与现在时间相差秒数
        val timeGap = (currentTime / 1000 - timeStamp)
        var timeStr: String

        //
        val inputTime = Calendar.getInstance()
        inputTime.timeInMillis = timeStamp * 1000
        val date = inputTime.time
        var year = inputTime.get(Calendar.YEAR)
        var month =  inputTime.get(Calendar.MONTH)+1
        var day =inputTime.get(Calendar.DAY_OF_MONTH)
        var hour = inputTime.get(Calendar.HOUR_OF_DAY)
        val mintues = inputTime.get(Calendar.MINUTE)
        //
//        Log.e(TAG, "getChatTimeStr2: ====>>time:"+year+"-"+ month+"-"+day)
        ///////////////////////
        timeStr=if(timeGap< MINUTE){

            "刚刚"
        }else if(MINUTE<=timeGap&&timeGap< HOUR ){

            (timeGap / MINUTE).toString() + "分钟前"
        }else if(HOUR<=timeGap&&timeGap<DAY){

            if(day==DateUtil.currentMonthDay){//当天
//                val sdf = SimpleDateFormat("M" + "月" + "d" + "日" + " HH:mm")
                val sdf = SimpleDateFormat("HH:mm")
                sdf.format(date)
            }else{
                val sdf = SimpleDateFormat("HH:mm")
                "昨天 "+sdf.format(date)
            }
        }else if(DAY<=timeGap&&timeGap< YEAR){


//            yyyy-M-d HH:mm:ss
            if(year==DateUtil.year){//今年
//                Log.e(TAG, "getChatTimeStr2: ====>>hello01" )
                val sdf = SimpleDateFormat("MM-dd HH:mm")
                sdf.format(date)
            }else{
//                Log.e(TAG, "getChatTimeStr2: ====>>hello02:"+DateUtil.year+"   :"+year )
                val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm")
                sdf.format(date)
            }
        }else{
//            Log.e(TAG, "getChatTimeStr2: ====>>hello05", )
            val sdf = SimpleDateFormat("yyyy-M-D HH:mm")
            sdf.format(date)
        }


        return timeStr
    }


    /**
     * 比较两个时间搓的间隔
     * 输出间隔时间毫秒单位
     * */
    @JvmStatic
    fun getTimeInterval(currentTime: Long, plan_time: Long): Long {
        return currentTime - plan_time
    }


    /**
     * 获取该时间戳往前Y天加N个小时的时间戳
     */
    @JvmStatic
    fun getBeforeTimeStamp(plan_time: Long, day: Int, hour: Int): Long {
        var result = plan_time
        val minute: Long = 60 //s
        val oneHour = 60 * minute //m
        val oneDay = 24 * oneHour //h
        result -= oneDay * day + oneHour * hour
        return result
    }

    /**
     * 获取两个时间戳的间隔天数
     * currentTime毫秒级
     */
    @JvmStatic
    fun getIntervalDay(currentTime: Long, plan_time: Long): Int { //
        var result = 0
        val interval = plan_time - currentTime / 1000
        val minute: Long = 60 //s
        val oneHour = 60 * minute //m
        val oneDay = 24 * oneHour //h
        if (interval > oneDay) {
            result = (interval / oneDay).toInt()
        }
        return result
    }

    /**
     * 参数为秒
     * 返回 m:s后
     */
    @JvmStatic
    fun getSecondText3(time: Int): String { //30s====>>00:30
        return String.format("%02d m: %02d s", time / 60 * 60, time % 60)
    }

    //参数秒级
    /**
     * 30s====>>00:30
     * */
    @JvmStatic
    fun getSecondText2(time: Long): String { //
        return String.format("%02d:%02d", time / 60, time % 60)
        //        StringBuffer str = new StringBuffer();
//        if (time <= 60) {
//            if (time < 10) {
//                str.append("00:0" + time);
//            } else {
//                str.append("00:" + time);
//            }
//
//        } else {
//            //其他的后期加上
//        }
//
//        return str.toString();
    }


    /**
     * 参数为秒
     * 返回 h:m:s后
     */
    @JvmStatic
    fun getSecondText4(time: Int): String { //30s====>>00:00:30
        return String.format("%02d h :%02d m :%02d s", time / (60 * 60 * 60),time /( 60 * 60), time % 60)
    }

    /**
     * 参数 秒      70
     * 返回 d:h:m:s     00:00:01:10
     */
    @JvmStatic
    fun getSecondText(second: Int): String { //00:00:52:00
        val result = StringBuffer()
        val day = second / (3600 * 24)
        val hour = (second - day * (3600 * 24)) / 3600
        val min = (second - hour * 3600 - day * (3600 * 24)) / 60
        val se = second % 60
        if (day > 0) {
            result.append(day)
            result.append("D")
        }
        if (hour > 0) {
            result.append(hour)
            result.append("h")
        }
        if (min > 0) {
            if (min < 10) {
                result.append("0$min")
            } else {
                result.append(min)
            }
            result.append("m")
        }
        if (se < 10) {
            result.append("0$se")
        } else {
            result.append(se)
        }
        result.append("s")
        return result.toString()
    }



    /**
     * 参数必须是秒级别的
     * */
    @JvmStatic
    fun stampToDate(s: String): String {
        val result: String
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd") //"yyyy-MM-dd HH:mm:ss"
        val lt = s.toLong()
        result = simpleDateFormat.format(lt * 1000)
        return result
    }

    //   将时间戳转换为时间   *1000  参数必须是毫秒级别的 *1000
    @JvmStatic
    fun stampToDate2(s: Long): String {
        val result: String
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss") //"yyyy-MM-dd HH:mm:ss"
        result = simpleDateFormat.format(s)
        return result
    }

    //   将时间戳转换为Date  1520402475
    @JvmStatic
    @Throws(ParseException::class)
    fun stampToDate3(s: String?): Date? { //fail
        var date: Date? = null
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss") //"yyyy-MM-dd HH:mm:ss"
        date = simpleDateFormat.parse(s)

        return date
    }

    //   将时间转换为时间戳
    @Throws(ParseException::class)
    @JvmStatic
    fun dateToStamp(s: String?): String {
        val result: String
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd") //"yyyy-MM-dd HH:mm:ss"
        val date = simpleDateFormat.parse(s)
        val ts = date.time
        result = ts.toString()
        return result
    }

    @JvmStatic
    fun getMonthDays(year: Int, month: Int): Int { //获取该月的天数
        var year = year
        var month = month
        if (month > 12) {
            month = 1
            year += 1
        } else if (month < 1) {
            month = 12
            year -= 1
        }
        val arr = intArrayOf(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
        var days = 0
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            arr[1] = 29 // 闰年2月29天
        }
        try {
            days = arr[month - 1]
        } catch (e: Exception) {
            e.stackTrace
        }
        return days
    }

    //201712
    val dateString: String
        get() { //201712
            val calendar = Calendar.getInstance()
            val month = calendar[Calendar.MONTH] + 1
            val month_ = if (month < 10) "0$month" else "" + month
            return "" + calendar[Calendar.YEAR] + month_ + "/"
        }
    val year: Int
        get() = Calendar.getInstance()[Calendar.YEAR]
    val month2: Int
        get() = Calendar.getInstance()[Calendar.MONTH]
    val month: Int
        get() = Calendar.getInstance()[Calendar.MONTH] + 1
    val currentMonthDay: Int
        get() = Calendar.getInstance()[Calendar.DAY_OF_MONTH]
    val weekDay: Int
        get() = Calendar.getInstance()[Calendar.DAY_OF_WEEK]
    val hour: Int
        get() = Calendar.getInstance()[Calendar.HOUR_OF_DAY]
    val minute: Int
        get() = Calendar.getInstance()[Calendar.MINUTE]

    @JvmStatic
    fun getWeekSunday(year: Int, month: Int, day: Int, pervious: Int): IntArray {
        val time = IntArray(3)
        val c = Calendar.getInstance()
        c[Calendar.YEAR] = year
        c[Calendar.MONTH] = month
        c[Calendar.DAY_OF_MONTH] = day
        c.add(Calendar.DAY_OF_MONTH, pervious)
        time[0] = c[Calendar.YEAR]
        time[1] = c[Calendar.MONTH] + 1
        time[2] = c[Calendar.DAY_OF_MONTH]
        return time
    }

    @JvmStatic
    fun getWeekDayFromDate(year: Int, month: Int): Int {
        val cal = Calendar.getInstance()
        cal.time = getDateFromString(year, month)
        var week_index = cal[Calendar.DAY_OF_WEEK] - 1
        if (week_index < 0) {
            week_index = 0
        }
        return week_index
    }

    @JvmStatic
    fun getDateFromString2(year: Int, month: Int, day: Int): Date? {
        val dateString = (year.toString() + "-" +
                (if (month > 9) month else "0$month") + "-"
                + (if (day > 9) day else "0$day") + " "
                + " 00:00:01")
        var date: Date? = null
        try {
            val sdf = SimpleDateFormat("yyyy-MM-dd")
            date = sdf.parse(dateString)
        } catch (e: ParseException) {
            println(e.message)
        }
        return date
    }

    @JvmStatic
    fun getDateFromString(year: Int, month: Int): Date? {
        val dateString = year.toString() + "-" + (if (month > 9) month else "0$month") + "-01"
        var date: Date? = null
        try {
            val sdf = SimpleDateFormat("yyyy-MM-dd")
            date = sdf.parse(dateString)
        } catch (e: ParseException) {
            println(e.message)
        }
        return date
    }

    /**
     * 解析日期
     * yyyy-MM-dd HH:mm:ss   ====>>date
     */
    @JvmStatic
    fun parseDateTime(datetime: String?): Date? {
        var date: Date? = null
        try {
            date = formatDateTime.parse(datetime)
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return date
    }

    /**
     * 以友好的方式显示时间2
     * lastTime 秒级别
     */
    @JvmStatic
    fun friendlyTime2(compareTime: String): String {
        var result = ""
        val lastTime = compareTime.toLong()
        val currentTime = System.currentTimeMillis()
        val interval = currentTime / 1000 - lastTime //second 间隔秒
        val minute: Long = 60 //s
        val oneHour = (60 * 60).toLong() //s
        val halfDay = 12 * oneHour //h
        val oneDay = 24 * oneHour //h

        result = if (interval > oneDay) {
            //今年非今年
            val calendar = longToCalendar(lastTime)
            if(calendar.get(java.util.Calendar.YEAR)==DateUtil.year){
                longToString(lastTime*1000,FORMAT_MONTH_DAY_TIME)
            }else{
                longToString(lastTime*1000,FORMAT_DATE_TIME03)
            }

        } else if (interval < oneDay && interval >= halfDay) {
            longToString(lastTime*1000,FORMAT_MONTH_DAY_TIME)
        } else if (interval < halfDay && interval >= oneHour) {
            (interval / oneHour).toString() + " 小时前"
        } else if (interval < oneHour && interval >= minute) {
            (interval / minute).toString() + " 分钟前"
        } else if (interval < minute) {
            "刚刚"
        } else {
            " "
        }
        return result
    }

    /**
     * 以友好的方式显示时间
     */
    @JvmStatic
    fun friendlyTime(timeStamp: Long): String { //sdate必须是毫秒级别的  *1000   success
        val inputTime = Calendar.getInstance()
        inputTime.timeInMillis = timeStamp

        val time = inputTime.time ?: return "Unknown"
        var ftime = ""
        val cal = Calendar.getInstance()
        // 判断是否是同一天
        val curDate = formatDate.format(cal.time)
        val paramDate = formatDate.format(time)
        if (curDate == paramDate) {
            val hour = ((cal.timeInMillis - time.time) / 3600000).toInt()
            ftime = if (hour == 0) {
                Math.max((cal.timeInMillis - time.time) / 60000, 1).toString() + "分钟前"
            } else {
                hour.toString() + "小时前"
            }
            return ftime
        }
        val lt = time.time / 86400000
        val ct = cal.timeInMillis / 86400000
        val days = (ct - lt).toInt()
        if (days == 0) {
            val hour = ((cal.timeInMillis - time.time) / 3600000).toInt()
            ftime = if (hour == 0) {
                Math.max((cal.timeInMillis - time.time) / 60000, 1).toString() + "分钟前"
            } else {
                hour.toString() + "小时前"
            }
        } else if (days == 1) {
            ftime = "昨天"
        } else if (days == 2) {
            ftime = "前天"
        } else if (days > 2 && days <= 10) {
            ftime = days.toString() + "天前"
        } else if (days > 10) {
            ftime = formatDate.format(time)
        }
        return ftime
    }

    /**
     * 根据日期获取当期是周几
     */
    @JvmStatic
    fun getWeek(date: Date?): String {
        val weeks = arrayOf("周日", "周一", "周二", "周三", "周四", "周五", "周六")
        val cal = Calendar.getInstance()
        cal.time = date
        var week_index = cal[Calendar.DAY_OF_WEEK] - 1
        if (week_index < 0) {
            week_index = 0
        }
        return weeks[week_index]
    }

    /**
     * 时间显示格式1
     * 5时30分
     * 30分钟
     *
     * */
    fun getSecondStr4(second: Int): Array<Int?>? {
        var result = "";
        val ss2 = arrayOfNulls<Int>(2)
//        var day=second/3600*24
        var hour = second / 3600
        var min = (second - hour * 3600) / 60
        var se = (second % 60)

        if (hour <= 0) {
            if (se >= 30) {
                min += 1//四舍五入
            }
            result = "" + min + " 分钟"

            ss2[0] = 0
            ss2[1] = min
        } else {
            if (se >= 30) {
                min += 1//四舍五入
            }
            result = "" + hour + " 时 " + min + " 分钟"

            ss2[0] = hour
            ss2[1] = min
        }
//        Log.e(TAG, "getSecondStr4: ====>>" + second)

        return ss2
    }

    /**
     * 时间显示格式3
     * 6时5分   四舍五入
     * */
    fun getSecondStr3(second: Int): String {
        var result = "";

//        var day=second/3600*24
        var hour = second / 3600
        var min = (second - hour * 3600) / 60
        var se = (second % 60)

        if (second < 30) {
            result = "0 分钟"

        } else if (30 <= second && second < 90) {
            result = "1 分钟"

        } else if (90 <= second && second < 3600) {
            result = "" + min + " 分钟"

            if (se >= 30) {
                result = "" + (min + 1) + " 分钟"
            }

        } else {
            result = "" + hour + " 时 " + min + " 分 "

            if (se >= 30) {
                result = "" + hour + " 时 " + (min + 1) + " 分 "
            }
        }
        return result
    }

    /**
     * 时间显示格式2
     * 12:00:03
     * */
    fun getSecondStr2(second: Int): String {
        var result = "";


        var hour = second / 3600
        var min = (second - hour * 3600) / 60
        var se = (second % 60)

        var hour_str = if (hour < 10) {
            "0" + hour
        } else {
            "" + hour
        }
        var min_str = if (min < 10) {
            "0" + min
        } else {
            "" + min
        }
        var second_str = if (se < 10) {
            "0" + se
        } else {
            "" + se
        }

        if (second < 60) {
            result = "00:00:" + second_str

        } else if (second >= 60 && second < 3600) {
            result = "00:" + min_str + ":" + second_str
        } else {
            result = hour_str + ":" + min_str + ":" + second_str + ""
        }

//        Log.e(TAG, "getSecondStr2: ====>>:" + second + "   reslut:" + result)
        return result
    }


    /**
     * 时间显示格式1
     * 5分30秒
     * */
    fun getSecondStr1(second: Int): String {
        var result = "";

//        var day=second/3600*24
        var hour = second / 3600
        var min = (second - hour * 3600) / 60
        var se = (second % 60)

        if (second < 60) {
            result = "" + se + " 秒"

        } else if (60 <= second && second < 3600) {
            result = "" + min + " 分 " + se + " 秒"

        } else {
            result = "" + hour + " 时 " + min + " 分 " + se + " 秒"
        }
        return result
    }

}